package top.zhdev.community.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import top.zhdev.community.entity.Page;
import top.zhdev.community.entity.User;
import top.zhdev.community.service.FollowService;
import top.zhdev.community.service.UserService;
import top.zhdev.community.utils.CommunityConstant;
import top.zhdev.community.utils.HostHolder;

import java.util.List;
import java.util.Map;

/**
 * 关注处理器
 */
@Controller
public class FollowController implements CommunityConstant {
    private UserService userService;
    private FollowService followService;
    private HostHolder hostHolder;

    @Autowired
    public void setUserService(UserService userService){
        this.userService = userService;
    }

    @Autowired
    public void setFollowService(FollowService followService){
        this.followService = followService;
    }

    @Autowired
    public void setHostHolder(HostHolder hostHolder){
        this.hostHolder = hostHolder;
    }

    /**
     * 查询该用户关注的实体数量
     * @param userId
     * @param page
     * @param model
     * @return
     */
    @GetMapping("/followees/{userId}")
    public String getFollowees(@PathVariable("userId") int userId, Page page, Model model){
        User user = userService.getUserById(userId);
        if(user == null){
            throw new RuntimeException("该用户不存在");
        }
        model.addAttribute("user",user);
        page.setLimit(5);
        page.setPath("/followees/" + userId);
        int followeeCount = (int)followService.getFolloweeCount(userId,ENTITY_TYPE_USER);
        page.setRows(followeeCount);

        List<Map<String,Object>> userList = followService.getFollowees(userId,page.getOffset(),page.getLimit());
        if(userList != null){
            for(Map<String,Object> map : userList){
                User u = (User) map.get("user");
                map.put("hasFollowed",hasFollowed(u.getId()));
                // userList中的对象是map，引用类型，因此修改后就直接变化了
            }
        }
        model.addAttribute("users",userList);
        return "/site/followee";
    }

    /**
     * 获取该用户的粉丝信息
     * @param userId
     * @param page
     * @param model
     * @return
     */
    @GetMapping("/followers/{userId}")
    public String getFollowers(@PathVariable("userId") int userId,Page page,Model model){
        User user = userService.getUserById(userId);
        if(user == null){
            throw new RuntimeException("该用户信息不存在");
        }
        model.addAttribute("user",user);
        page.setLimit(5);
        page.setPath("/followers/" + userId);
        int followerCount = (int)followService.getFollowerCount(userId,ENTITY_TYPE_USER);
        page.setRows(followerCount);

        List<Map<String,Object>> userList = followService.getFollowers(userId,page.getOffset(),page.getLimit());
        if(userList != null){
            for (Map<String,Object> map : userList){
                User u = (User) map.get("user");
                map.put("hasFollowed",hasFollowed(u.getId()));
            }
        }

        model.addAttribute("users",userList);
        return "/site/follower";
    }

    /**
     * 查询该用户是否关注了某实体
     * @param userId
     * @return
     */
    private boolean hasFollowed(int userId){
        if(hostHolder.getUser() == null){
            return false;
        }

        return followService.hasFollowed(hostHolder.getUser().getId(),ENTITY_TYPE_USER,userId);
    }
}
